<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="学科：">
        <el-input v-model="queryParam.subjectName" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
        <el-button type="primary" @click="handleEdit(0)">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column prop="id" label="Id" />
      <el-table-column prop="name" label="学科" />
      <el-table-column prop="info" label="介绍" />
      <el-table-column width="220px" label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="medium ">编辑</el-button>
          <el-button size="mini" type="danger" @click="delSubject(scope.row)" class="link-left">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParam.pageIndex"
      :limit.sync="queryParam.pageSize"
      @pagination="search"
    />
    <!-- 编辑信息 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="pojo">
        <el-form-item label="学科">
          <el-input v-model="pojo.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input v-model="pojo.info" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import Pagination from "@/components/Pagination";
import subjectApi from "@/api/subject";

export default {
  components: { Pagination },
  data() {
    return {
      queryParam: {
        subjectName: null,
        pageIndex: 1,
        pageSize: 10
      },
      listLoading: true,
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      pojo: {},
      title: "添加",
      id: 0
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      this.listLoading = true;
      subjectApi.pageList(this.queryParam).then(data => {
        const re = data.response;
        this.tableData = re.list;
        this.total = re.total;
        this.queryParam.pageIndex = re.pageNum;
        this.listLoading = false;
      });
    },
    submitForm() {
      this.queryParam.pageIndex = 1;
      this.search();
    },
    delSubject(row) {
      let _this = this;
      subjectApi.deleteSubject(row.id).then(re => {
        if (re.code === 1) {
          _this.search();
          _this.$message.success(re.message);
        } else {
          _this.$message.error(re.message);
        }
      });
    },
    handleEdit(id) {
      this.dialogFormVisible = true; // 打开窗口

      if (id != 0) {
        // 修改
        this.title = "编辑";
        this.id = id;
        subjectApi.select(id).then(response => {
          if (response.code == 1) {
            this.pojo = response.response;
          }
        });
      } else {
        this.title = "添加";
        this.pojo = {}; // 清空数据
      }
    },
    handleSave() {
      // 编辑，新增
      this.pojo.id = this.id;
      let _this = this;
      subjectApi.edit(this.pojo).then(re => {
        if (re.code === 1) {
          _this.search();
          _this.$message.success(re.message);
        } else {
          _this.$message.error(re.message);
        }
        _this.dialogFormVisible = false;
      });
    }
  },

  computed: {
    ...mapGetters("enumItem", ["enumFormat"]),
    ...mapState("enumItem", {
      levelEnum: state => state.user.levelEnum
    })
  }
};
</script>
